// pages/binding/index.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');
var setTimer = '';
var countdownTime = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    mobile: "",
    password: "",
    repassword: "",
    sendBtnStr: "发送验证码",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  formSubmit: function (e) {

    var that = this;

    var formData = e.detail.value;

    console.log(formData);

    if (formData.mobile == "") {
      wx.showModal({
        title: '请填写手机号',
      });
      return;
    }

    if (formData.captcha == "") {
      wx.showModal({
        title: '请填写验证码',
      });
      return;
    }

    if (formData.password == ""||formData.password.length<6) {
      wx.showModal({
        title: '请正确设置密码',
      });
      return;
    }

    if (formData.repassword != formData.password) {
      wx.showModal({
        title: '请正确重复密码',
      });
      return;
    }

    var wx_userInfo = wx.getStorageSync("wx_userInfo");
    var openId = wx.getStorageSync("openId");

    formData["nickname"]=wx_userInfo.nickName;
    formData["avatar"]=wx_userInfo.avatarUrl;
    formData["openid"]=openId;
    formData["group_id"] = 19;

    // console.log(formData);
    // return;

    wx.showToast({
      title: '正在提交',
      icon: 'loading',
      duration: 2000,
      mask: true,
    });

    http.ApiPost(apiUrl.UrlRegister, formData, function (res) {

      if (res.result.errorCode == 1) {
        var user = res.userinfo;

        wx.showToast({
          title: '注册成功！',
          icon: 'success',
          duration: 5000
        })

        wx.setStorageSync("user", user);
        var token = user.token;
        wx.setStorageSync("token", token);

        wx.reLaunch({
          url: '../service/index'
        });

      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  getPhoneNumber: function (e) {
    var that = this;
    // console.log(e);
    var cloudID = e.detail.cloudID;
    console.log(cloudID);
    //调用云函数直接获取开放数据
    wx.cloud.callFunction({
      name: 'getOpenData',
      data: {
        weRunData: wx.cloud.CloudID(cloudID), // 这个 CloudID 值到云函数端会被替换
      },
      complete: res => {
        // console.log('callFunction result: ', res.result);
        var wx_phoneData = res.result.weRunData.data;
        wx.setStorageSync("wx_phoneData", wx_phoneData);

        that.setData({
          mobile: wx_phoneData.phoneNumber
        })
      }
    })
  },
  mobileInput: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  repasswordInput: function (e) {
    this.setData({
      repassword: e.detail.value
    })
  },
  protocolTap: function () {
    wx.navigateTo({
      url: '../protocol/index'
    });
  },
  sendCode: function () {
    var that = this;
    var mobile = that.data.mobile;
    if (mobile == "") {
      wx.showModal({
        title: '请填写手机号',
      });
      return;
    }
    console.log(mobile);

    //开启倒计时
    if (countdownTime == 0) {
      countdownTime = 60;
      that.countdown();

      http.ApiPost(apiUrl.UrlCodeSend, {
        "mobile": mobile
      }, function (res) {

        if (res.code == 1) {

          wx.showToast({
            title: '发送成功！',
            icon: 'success',
            duration: 5000
          })

        } else {
          wx.showModal({
            title: res.msg
          });
        }
      });
    }
  },
  countdown: function () {
    var that = this;
    if (countdownTime == 0) {
      that.setData({
        sendBtnStr: "发送验证码"
      })
      //结束倒计时
      clearTimeout(setTimer);
    } else {
      that.setData({
        sendBtnStr: countdownTime + "秒后重试",
      })
      countdownTime--;
      //重复
      setTimer = setTimeout(function () {
        that.countdown();
      }, 1000);
    }
  },
})